
package aa.util;

import java.io.Serializable;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;

/**
 *  This class defines a security key to limit the access right 
 *  for a certain object or operation.
 * 
 *  <p>
 *  <b>History:</b>
 *  <ul>
 * 	<li> February 19, 2004 - version 0.0.4
 * 	<ul>
 * 	    <li> do minor modification.
 * 	</ul>
 *      <li> May 9, 2003 - version 0.0.3
 *      <ul>
 *          <li> modify internal comments.
 *      </ul>
 *      <li> Apr 12, 2003 - version 0.0.2
 *      <ul>
 *          <li> modify this file.
 *      </ul>
 *      <li> Dec 11, 2001 - version 0.0.1
 *      <ul>
 *          <li> create this file.
 *      </ul>
 *  </ul>
 *
 *  @author Myeong-Wuk Jang
 *  @version $Date: 2008/01/01 00:24:05 $ $Revision: 1.1 $
 */

public class Key implements Serializable
{
    // ========== Object Variables ========== //
    
    /**
     *  Indentifer field of this key. 
     *  This value is generated by a random rumber generator.
     */
    private long m_lID;
    
    /**
     *  Time field of this key.
     *  This value specifies when this key is generated.
     */
    private Calendar m_calTime;
    

    // ========== Object Methods ========== //

    /**
     *  Creates a secure key.
     */
    public Key()
    {
	m_calTime = new GregorianCalendar();

	Random rnd = new Random(m_calTime.getTimeInMillis());
	m_lID   = rnd.nextLong();
    }


    /**
     *  Indicates whether some other key is 'equal to' this key.
     * 
     *  @param p_key the reference key to be compared with this key
     * 
     *  @return <code>true</code> if this key is the same as the the reference key;<br>
     *          <code>false</code> otherwise.
     */
    public boolean equals(Key p_key)
    {
    	if (p_key == null) {
    	    return false;
    	}
    	
	if ( (m_lID == p_key.getID()) &&
	     (m_calTime.equals(p_key.getTime())) ) {
	    return true;
	} else {
	    return false;
	}
    }


    /**
     *  Returns the assigned identifier of this key.
     * 
     *  @return the assigned identifier of this key.
     */
    public final long getID()
    {
	return m_lID;
    }


    /**
     *  Returns the assigend time value of this key.
     * 
     *  @return the assigend time value of this key.
     */
    public final Calendar getTime()
    {
	return m_calTime;
    }
    
    
    /**
     *  Returns a string representation of this object.
     * 
     *  @return a string representation of this object.
     */
    public String toString()
    {
    	return "key://" + m_lID + ":" + m_calTime;
    }
}